#region Using directives
using System;
using System.Data ;
using WP.DB;
using WP.MF.DataEntity; 
using WP.Commons;

#endregion
namespace WP.MF.DataAccess
{

	/*
	 * Created by SmartCreateCode Tool
	 * At 4/21/2005
	 */

    public class MCurrencyDAL : IDataAccess
	{
		public MCurrencyDAL()
		{
		}

        #region Select List
        public string SQLList(int iParameter)
        {
            string strSQL = @"SELECT RecId,CurNo,CurCode,CurRateSellProduct,CurRatePurchProduct,CurDesc FROM mCurrency ORDER BY CurNo ";
            return strSQL;
        }
        #endregion

		#region Fields
		public string Fields
		{
			get
			{
				return @"CurNo,CurCode,CurDesc,CurRateSellProduct,CurRatePurchProduct,LastRateChgDate,arAcctNo,arProfitNo,arExchAcctNo,arExchProfitNo,
                    arDiscAcctNo,arDiscProfitNo,apAcctNo,apProfitNo,apExchAcctNo,apExchProfitNo,apDiscAcctNo,apDiscProfitNo,AccruAcctNo,AccruProfitNo,
                    BankAcctNo,BankProfitNo,BankExchAcctNo,BankExchProfitNo,apBankAcctNo,apBankProfitNo,InterestAcctNo,InterestProfitNo,SettDiscAcctNo,SettDiscProfitNo,
					DepositAcctNo,DepositProfitNo,OverShortAcctNo,OverShortProfitNo,FrtRedtAcctNo,FrtRedtProfitNo,RebateDrAcctNo,RebateDrProfitNo,RebateCrAcctNo,RebateCrProfitNo,
                    InterCompFundTrfAcctNo,InterCompFundTrfProfitNo,InterCompFundTrfBranchNo,InvTrfAcctNo,InvTrfProfitNo,FreightInAccruAcctNo,FreightInAccruProfitNo,
                    FreightOutAccruAcctNo,FreightOutAccruProfitNo,HandingAccruAcctNo,HandingAccruProfitNo,RebateAccruAcctNo,RebateAccruProfitNo,RemanAccruAcctNo,RemanAccruProfitNo,
                    FreightApAcctNo,FreightApProfitNo,MillAPAcctNo,MillAPProfitNo,MiscAPAcctNo,MiscAPProfitNo,ApACHAcctNo,ApACHProfitNo,ReportCurRate,RealizationAcctNo,RealizationProfitNo,RealizationBrnNo,GainLossAcctNo,GainLossProfitNo";
			}
		}
		#endregion

        #region CurHist_Fields
        public string CurHist_Fields
        {
            get
            {
                return @"CurNo,CurRateSellProduct,CurRatePurchProduct,LastRateChgDate";
            }
        }
        #endregion


        #region Select MCurrency for look up or list
        public string SelectMCurrencyForLookUp()
		{
			return "SELECT CurNo,CurCode,CurDesc,CurRateSellProduct,CurRatePurchProduct FROM MCurrency";
		}
		#endregion

		#region Select MCurrency for list
		public string SelectMCurrencyForList()
		{
			return "SELECT CurNo,CurCode,recid FROM MCurrency";
		}
		#endregion

		#region select MCurrency for BindDDL in Im
		public string SelectMCurrencyForBindDDL()
		{
			return "Select CurNo,CurCode from mCurrency order by Curno";
		}
		#endregion

		#region Select by Key
		public MCurrencyDEL SelectMCurrencyByKey(DBAccess dBAccess, string iRecId)
		{
			MCurrencyDEL mCurrencyDEL=new MCurrencyDEL();
			string strSql=string.Empty;
			object[] oValues;

			strSql= @"SELECT RecId,CurNo,CurCode,CurDesc,CurRateSellProduct,
				CurRatePurchProduct,arAcctNo,arProfitNo,arBrnNo,arExchAcctNo,
				arExchProfitNo,arExchBrnNo,apAcctNo,apProfitNo,apBrnNo,
				apExchAcctNo,apExchProfitNo,apExchBrnNo,BankAcctNo,BankProfitNo,
				BankBrnNo,BankExchAcctNo,BankExchProfitNo,BankExchBrnNo,arDiscAcctNo,
				arDiscProfitNo,arDiscBrnNo,InterestAcctNo,InterestProfitNo,InterestBrnNo,
				apDiscAcctNo,apDiscProfitNo,apDiscBrnNo,LastChqNo,AccruAcctNo,
				AccruProfitNo,apBankAcctNo,apBankProfitNo,apBankBrnNo,LastRateChgDate,
				SettDiscAcctNo,SettDiscProfitNo,SettDiscBrnNo,arGstAcctNo,arGstProfitNo,
				arGstBrnNo,apGstAcctNo,apGstProfitNo,apGstBrnNo,RebateCrAcctNo,
				RebateCrProfitNo,RebateDrAcctNo,RebateDrProfitNo,DepositAcctNo,DepositProfitNo,
				DepositBrnNo,OverShortAcctNo,OverShortProfitNo,OverShortBrnNo,FrtRedtAcctNo,
				FrtRedtProfitNo,LastChqNo1,InterCompFundTrfAcctNo,InterCompFundTrfProfitNo,InterCompFundTrfBranchNo,InvTrfAcctNo,InvTrfProfitNo,RealizationAcctNo,RealizationProfitNo,RealizationBrnNo,RecStamp  FROM MCurrency
				WHERE RecId={0}";

			oValues=new object[1];
			oValues[0]=iRecId;
			strSql=string.Format(strSql,oValues);
			dBAccess.GetDataSet(strSql,MCurrencyDEL.TABLENAME_MCURRENCY,mCurrencyDEL);
			return mCurrencyDEL;
		}
		#endregion

		#region UpdateMCurrencyHist
		public void UpdateMCurrencyHist(DBAccess dBAccess,MCurrencyDEL mCurrencyDEL)
		{
			string strSql=string.Empty;
			int iIndex=0;
			object[] oValues;

			DataRow DR=mCurrencyDEL.Tables[MCurrencyDEL.TABLENAME_MCURRENCY].Rows[0];
			strSql=@"UPDATE MCurHist SET CurRateSellProduct={0},CurRatePurchProduct={1}  WHERE CurNo='{2}' and LastRateChgDate='{3}'";

			oValues=new object[4];
			oValues[iIndex++]=DR[MCurrencyDEL.CURRATESELLPRODUCT].ToString();
			oValues[iIndex++]=DR[MCurrencyDEL.CURRATEPURCHPRODUCT].ToString();
			oValues[iIndex++]=DR[MCurrencyDEL.CURNO].ToString();
			oValues[iIndex++]=DR[MCurrencyDEL.LASTRATECHGDATE].ToString();

			strSql=string.Format(strSql,oValues);

			dBAccess.ExecuteSql(strSql);

		}
		#endregion UpdateMCurrencyHist

		#region InsertMCurrencyHist
        public void InsertMCurrencyHist(DBAccess dBAccess, MCurrencyDEL mCurrencyDEL)
        {
            string strSql = string.Empty;
            int iIndex = 0;
            object[] oValues;

            DataRow DR = mCurrencyDEL.Tables[MCurrencyDEL.TABLENAME_MCURRENCY].Rows[0];
            strSql = @"insert into MCurHist (CurRateSellProduct,CurRatePurchProduct,CurNo,LastRateChgDate,ChangeBy,ChangeDate) 
					values( {0}, {1} ,'{2}' ,'{3}','{4}' ,'{5}')";

            oValues = new object[6];
            oValues[iIndex++] = DR[MCurrencyDEL.CURRATESELLPRODUCT].ToString();
            oValues[iIndex++] = DR[MCurrencyDEL.CURRATEPURCHPRODUCT].ToString();
            oValues[iIndex++] = DR[MCurrencyDEL.CURNO].ToString();
            oValues[iIndex++] = DR[MCurrencyDEL.LASTRATECHGDATE].ToString();
            oValues[iIndex++] = SysVar.UserName;
            oValues[iIndex++] = DateTime.Now;

            strSql = string.Format(strSql, oValues);

            dBAccess.ExecuteSql(strSql);

            InsertReportCurHist(dBAccess, mCurrencyDEL);
        }
		#endregion InsertMCurrencyHist

        public void InsertReportCurHist(DBAccess dBAccess, MCurrencyDEL mCurrencyDEL)
        {
            if (!DataFunctions.GetmSwitchValue(dBAccess, "UseReportingCurrencyInGL", "M")) return;

            string strSql = string.Empty;
            int iIndex = 0;
            object[] oValues;

            DataRow DR = mCurrencyDEL.Tables[MCurrencyDEL.TABLENAME_MCURRENCY].Rows[0];
            strSql = @"insert into MReportCurHist (CurRate,CurNo,LastRateChgDate) 
					values( {0}, '{1}' ,'{2}' )";

            oValues = new object[3];
            oValues[iIndex++] = DR[MCurrencyDEL.ReportCurRate].ToString();            
            oValues[iIndex++] = DR[MCurrencyDEL.CURNO].ToString();
            oValues[iIndex++] = DR[MCurrencyDEL.LASTRATECHGDATE].ToString();

            strSql = string.Format(strSql, oValues);

            dBAccess.ExecuteSql(strSql);
        }

		#region Delete table by Key
		public void DeleteMCurrency(DBAccess dBAccess,string strRecId)
		{
			string strSql=string.Empty;
            string strCurNo = DataFunctions.GetFieldValue(dBAccess, "MCurrency", "CurNo", "recid=" + strRecId);
			strCurNo=BaseFunctions.SpecialCharDeal( strCurNo );

            strSql = @"DELETE FROM mReportCurHist WHERE CurNo ='" + strCurNo + "';DELETE FROM mCurHist WHERE CurNo ='" + strCurNo + "';DELETE FROM mCurrency WHERE RecId=" + strRecId;

			dBAccess.ExecuteSql(strSql);

           
		}
		#endregion


	}
}